layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;


    $.get(ctx + "/user/queryAllSales", function (res) {
        for (var i = 0; i < res.length; i++) {
            if ($("input[name='assignManFlag']").val() == res[i].rowId) {
                $("#assignMan").append("<option value=\"" + res[i].rowId + "\"  selected='selected' >" + res[i].userName + "</option>");
            } else {
                $("#assignMan").append("<option value=\"" + res[i].rowId + "\"   >" + res[i].userName + "</option>");
            }
        }
        // 重新渲染下拉框内容
        layui.form.render("select");
    });


    form.on('submit(addOrUpdateSaleChance)', function (data) {
        var index = top.layer.msg("数据提交中,请稍后..."
            , {
                icon: 16//图标
                , time: false//不定时关闭
                , shade: 0.8
            });//背景透明
        var url = ctx + "/sale_chance/save";
        type = "post";
        if ($("input[name='rowId']").val()) {
            url = ctx + "/sale_chance/update";
            type = "put";
        }
        $.ajax({
            url: url,
            data: data.field,
            type: type,
            success: function (res) {
                if (res.code == 200) {
                    layer.msg("操作成功",{icon: 6});
                    /**
                     * 关闭加载： 句柄 index
                     */
                    top.layer.close(index);
                    layer.closeAll("iframe");
                    // 刷新父页面，重新渲染表格数据，父窗口是因为当前在小小弹窗啊~
                    parent.location.reload();
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });
        /**
         * 阻止表单提交
         */
        return false
    });


    $("#close_btn").click(function () {
        var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
        parent.layer.close(index);//关闭iframe的弹窗
    })
});